import Vue from 'vue'
import Router from 'vue-router'
import Resource from 'vue-resource'
import App from './App'

import store from './vuex'
import components from './components'
import filters from './filters'
import routes from './routers'

Vue.use(Resource)
Vue.use(Router)

Vue.use(filters)
Vue.use(components)

Vue.http.options.emulateJSON = true
Vue.http.options.crossOrigin = true

Vue.http.interceptors.push((request, next) => {
  const token = window.sessionStorage.getItem('token')
  if (token) request.headers.set('Authorization', 'Bearer ' + token)
  next((response) => {
    if (response.data && response.data.errcode === 401) window.location.href = '/'
  })
})

const router = new Router({
  mode: 'history',
  routes,
  linkActiveClass: "active"
});

router.beforeEach((to, from, next) => {
  if (to.name !== "Login") {
    let token = window.sessionStorage.getItem('token')
    if (!token) next('/login')
    else next()
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App)   //es6 render: (function(h){return: h(app)})
}).$mount('#app')




// 入口文件